//
//  RDHomeViewController.m
//  ReverseDiabetes
//
//  Created by 王佩璇 on 2024/8/17.
//

#import "RDHomeViewController.h"
#import "RDAddBloodSugarViewController.h"
#import "RDDataExcelViewController.h"
#import "RDSettingViewController.h"

#import "LTSCalendarView.h"
#import "RDHomeNavView.h"

#import "RDHomeTodayTableViewCell.h"
#import "RDHomeHistoryTitleTableViewCell.h"
#import "RDHomeHistoryTableViewCell.h"
#import "RDHomeHistoryNullTableViewCell.h"


@interface RDHomeViewController ()<LTSCalendarEventSource,UITableViewDataSource,UITableViewDelegate,GRDHomeNavViewDelegate,RDHomeHistoryTableViewCellDelegate>{
    NSMutableDictionary *eventsByDate;
}

@property (nonatomic, strong) RDHomeNavView *navView;

@property (nonatomic, strong) NSMutableArray *bloodSugarHistoryArr;

@property (nonatomic, strong) NSMutableArray *bloodSugarTodayArr;
@end

@implementation RDHomeViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.bloodSugarHistoryArr = [[NSMutableArray alloc]init];
    
//    [self navButtonImageName:@"记录" targer:@"dataExcelAction" isRight:YES color:UIColor.clearColor];
    
    self.hbd_barHidden = YES;
    
    
    self.navView = [RDHomeNavView setupXibView];
    self.navView.delegate = self;
    self.navView.frame = CGRectMake(0, 0, SCREEN_WIDTH, STATUS_AND_NAVIGATION_HEIGHT);
    [self.view addSubview:self.navView];
    
    
    
    
    
    [self setupAddBtn];
    
  
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    [self reloadUIByDBData];
}

-(void)reloadUIByDBData{
    self.bloodSugarHistoryArr = [[RDBloodSugarDBManager sharedManager] getBloodSugarHistory];
    
    NSString *nowTime = [[NSDate date] stringWithFormat:@"yyyy-MM-dd"];
    self.bloodSugarTodayArr = [[RDBloodSugarDBManager sharedManager] getBloodSugarByRecordingTime:nowTime];
    
    
    
    [self.calendarView.tableView reloadData];
}



-(void)dataExcelAction{
    RDDataExcelViewController *vc = [[RDDataExcelViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - GRDHomeNavViewDelegate
- (void)didNextMonthClick{
    [self.calendarView.calendar loadNextPage];
}

- (void)didPreviousMonthClick{
    [self.calendarView.calendar loadPreviousPage];
}

- (void)didRecordingClick{
    RDDataExcelViewController *vc = [[RDDataExcelViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)didSettingClick{
    RDSettingViewController *vc = [[RDSettingViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 添加按钮
-(void)setupAddBtn{
    
    
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addBtn.frame = CGRectMake(SCREEN_WIDTH - 80, SCREEN_HEIGHT-STATUS_AND_NAVIGATION_HEIGHT - 120, 66, 66);
    [addBtn setImage:IMG(@"添加") forState:UIControlStateNormal];
  
    [addBtn addTarget:self action:@selector(addButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn];
}

-(void)addButtonAction{
    RDAddBloodSugarViewController *vc = [[RDAddBloodSugarViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - LTSCalendarEventSource

- (void)lts_InitUI{
    [super lts_InitUI];
    self.calendarView.frame = CGRectMake(0, STATUS_AND_NAVIGATION_HEIGHT, self.view.frame.size.width, self.view.frame.size.height-STATUS_AND_NAVIGATION_HEIGHT);
    
    [self.calendarView showSingleWeekView:NO];
    [self.calendarView updateUISingleWeekView];
  
}


// 该日期是否有事件
- (BOOL)calendarHaveEvent:(LTSCalendarManager *)calendar date:(NSDate *)date
{

    NSString *key = [date stringWithFormat:@"yyyy-MM-dd"];
    
    for (RDBloodSugar *model in self.bloodSugarHistoryArr) {
        if ([model.recording_time isEqualToString:key]) {
            return YES;
        }
    }

    return NO;
}
//当前 选中的日期  执行的方法
- (void)calendarDidDateSelected:(LTSCalendarManager *)calendar date:(NSDate *)date
{
    
    
    
    NSString *key = [[self dateFormatter] stringFromDate:date];
    self.navView.selectDateLab.text =  key;
    
    self.bloodSugarTodayArr = [[RDBloodSugarDBManager sharedManager] getBloodSugarByRecordingTime:[date stringWithFormat:@"yyyy-MM-dd"]];
    
    [self.calendarView.tableView reloadData];
    
    NSArray *events = eventsByDate[key];
    //    self.title = key;
    
    if (events.count>0) {
        
        //该日期有事件    tableView 加载数据
    }
    
}

- (void)calendarDidLoadPage:(LTSCalendarManager *)calendar { 
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    switch (section) {
        case 2:{
            if (self.bloodSugarHistoryArr.count==0) {
                return 1;
            }
            return self.bloodSugarHistoryArr.count;
        }
        default:{
            return 1;
        }
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:{
            RDHomeTodayTableViewCell *cell = [RDHomeTodayTableViewCell cellForNibTableView:tableView];
            
            cell.bloodSugarTodayArr = self.bloodSugarTodayArr;
            
            return cell;
        }
            
            
        case 1:{
            RDHomeHistoryTitleTableViewCell *cell = [RDHomeHistoryTitleTableViewCell cellForNibTableView:tableView];
            return cell;
        }
            
            
        case 2:{
            if (self.bloodSugarHistoryArr.count==0) {
                RDHomeHistoryNullTableViewCell *cell = [RDHomeHistoryNullTableViewCell cellForNibTableView:tableView];
                    
                return cell;
            }
            RDHomeHistoryTableViewCell *cell = [RDHomeHistoryTableViewCell cellForNibTableView:tableView];
            cell.model = self.bloodSugarHistoryArr[indexPath.row];
            cell.delegate = self;
            return cell;
        }
            
        default:{
            UITableViewCell *cell =[UITableViewCell new];
            return cell;
        }
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    switch (indexPath.section) {
        case 0:{
            return 130;
        }
            
            break;
            
        case 1:{
            return 44;
        }
        case 2:{
            if (self.bloodSugarHistoryArr.count==0) {
                
                    
                return 250;
            }
            return 85;
        }
        default:{
            return 0;
        }
            break;
    }
    
    
}

#pragma mark - RDHomeHistoryTableViewCellDelegate
- (void)didDelClick:(NSInteger)row{
    WEAKSELF
    
    RDBloodSugar *hisModel =  self.bloodSugarHistoryArr[row];
    
    [[RDBloodSugarDBManager sharedManager] deleteBloodSugar:hisModel];
    
    
    [self.bloodSugarHistoryArr removeObjectAtIndex:row];
    
    for (int i = 0; i<self.bloodSugarTodayArr.count; i++) {
        RDBloodSugar *model = self.bloodSugarTodayArr[i];
        if (model.blood_sugar_id.intValue == hisModel.blood_sugar_id.intValue) {
            [self.bloodSugarTodayArr removeObjectAtIndex:i];
            break;
        }
    }
    
    
    [self.calendarView.tableView reloadData];
}

@end
